package com.braveman.im.ui.activity.message;

import android.graphics.Color;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.bean.ChangePasswordBean;
import com.braveman.im.bean.MyFlockChildBean;
import com.braveman.im.bean.VerificationCodeBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.thread.ValidateThread;
import com.braveman.im.utils.UtilsStyle;
import com.braveman.im.utils.ValidateUtils;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.vondear.rxtools.RxBarTool;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;


public class ResetPwdActivity extends BaseActivity {

    private EditText mTelView;
    private TextView mGetValidateBtn;
    private EditText mValidateView;
    private EditText mPwdView;
    private EditText mPwdViewNext;
    private CheckBox mSeePwdView;
    private CheckBox mSeePwdViewNext;
    private List<MyFlockChildBean> list;
    private String registerCode;


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("忘记密码");
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_reset_pwd;
    }

    @Override
    protected void initChildView() {
        mTelView = findViewById(R.id.et_activity_register_tel);
        mGetValidateBtn = findViewById(R.id.tv_activity_register_get_validate);
        mValidateView = findViewById(R.id.et_activity_register_validate);
        mPwdView = findViewById(R.id.et_activity_register_pwd);//忘记密码的登录密码
        mPwdViewNext = findViewById(R.id.et_activity_register_pwd_next);//忘记密码的确认密码
        mSeePwdView = findViewById(R.id.cb_activity_register_see_pwd);
        mSeePwdViewNext = findViewById(R.id.cb_activity_register_see_pwd_next);


    }

    @Override
    protected void initChildData() {

    }

    @Override
    protected void setChildViewListener() {
        mSeePwdView.setOnClickListener(this);
        mSeePwdViewNext.setOnClickListener(this);
        mGetValidateBtn.setOnClickListener(this);
        findViewById(R.id.btn_activity_register).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        final String tel = mTelView.getText().toString();
        String code = mValidateView.getText().toString();
        String pwd = mPwdView.getText().toString();
        String pwdNext = mPwdViewNext.getText().toString();

        switch (v.getId()) {
            case R.id.tv_activity_register_get_validate:
                if (!ValidateUtils.isTelValidate(tel)) return;
                // mGetValidateBtn.setEnabled(false);
                VerificationCodeBean verificationCodeBean = new VerificationCodeBean();
                showLoadingDialog();
                verificationCodeBean.mobile = tel;
                verificationCodeBean.captchaType = "findPassword";
                ApiManager.getApi().verificationCode(verificationCodeBean).enqueue(new NetCallBack<VerificationCodeBean>() {
                    @Override
                    protected void onSuccess(Call<VerificationCodeBean> call, Response<VerificationCodeBean> response) {
                        // TODO: 2017/10/20 验证码接口
                        if (response != null) {
                            VerificationCodeBean verificationCodeBean1 = response.body();
                            if (TextUtils.isEmpty(verificationCodeBean1.captcha)) {
                                mGetValidateBtn.setEnabled(false);
                                new ValidateThread(mHandler).start();
                            }
                            mGetValidateBtn.setTextColor(Color.parseColor("#bebebe"));
                            registerCode = verificationCodeBean1.captcha;
                            mValidateView.setText(registerCode);
                        }
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();

                    }
                });


                break;
            case R.id.btn_activity_register: //完成按钮
                if (!ValidateUtils.isPwdValidate(pwd)) return;
                // TODO: 2017/10/20 修改密码接口
                ChangePasswordBean changePasswordBean = new ChangePasswordBean();
                changePasswordBean.mobile = tel;
                changePasswordBean.captcha = registerCode;
                showLoadingDialog();
                ApiManager.getApi().changePassworBean(changePasswordBean).enqueue(new NetCallBack<ChangePasswordBean>() {
                    @Override
                    protected void onSuccess(Call<ChangePasswordBean> call, Response<ChangePasswordBean> response) {
                        if (response != null) {
//                            ChangePasswordBean changePasswordBean1 = response.body();
//                            changePasswordBean1.password = mPwdView.getText().toString();
//                            changePasswordBean1.newPassword = mPwdViewNext.getText().toString();
                        }
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
                //ActivityUtils.finishActivity(this);
                break;
            case R.id.cb_activity_register_see_pwd:
                if (mSeePwdView.isChecked()) {
                    mPwdView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mPwdView.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.cb_activity_register_see_pwd_next:
                if (mSeePwdViewNext.isChecked()) {
                    mPwdViewNext.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mPwdViewNext.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }

                break;
        }
    }

    @Override
    protected void handleMessage(BaseActivity context, Message msg) {
        switch (msg.what) {
            case 100:
                mGetValidateBtn.setText(msg.obj.toString());
                break;
            case 200:
                mGetValidateBtn.setTextColor(Color.parseColor("#6284dd"));
                mGetValidateBtn.setText("获取验证码");
                mGetValidateBtn.setEnabled(true);
                break;

        }
    }
}
